<?php
namespace Home\Controller;
use Think\Controller;
class ShoppingController extends Controller {
	public function __construct(){
		parent::__construct();
		if(!$_SESSION['user_is_login']){
			$this->error('还未登录，请登录',U('home/user/login'));
		}
	}
    public function addToCart(){
    	$user_id = $_SESSION['user_id'];
    	$product_id = I('post.product_id');
    	$num = I('post.num');
    	$where = array('user_id' => $user_id,'product_id' => $product_id);
    	if($cart = M('user_cart')->where($where)->find()){
    		M('user_cart')->where($where)->save(array('num' => $num));
    	}else{
    		M('user_cart')->add(array('user_id' => $user_id,'product_id' => $product_id,'num' => $num));    		
    	}
    	$this->success('添加购物车成功',U('/home/shopping/cartlist'));
    }

	public function cartlist(){
		$carts = M('user_cart')->where('user_id',$user_id)->select();
		$this->assign('user_id ='.$_SESSION['user_id']);
		$this->assign('carts',$carts);
		$this->display();
	}

    public function cartdel($id){
    	M('user_cart')->where('id ='.$id)->delete();
        $this->redirect('cartlist');
    }
    public function updateCart(){
    	$id = I('get.id');
    	$num = I('get.num');
    	if($num > 0){    		
    	M('user_cart')->where('id ='.$id)->save(array('num' => $num));
    	echo "ok";
    	}else{
    		echo "数量不能为负！";
    	}
    }
    public function cartdels(){
    	$ids = I('post.ids');
    	M('user_cart')->delete(implode(',', $ids));
    	echo "ok";
    }
     public function order(){
    		$address =  M('user_address')->where('user_id',$_SESSION['user_id'])->select();
    		$this->assign('data',$_SESSION['preorder']);
    		$this->assign('address',$address);
    		$this->display();
    }
    public function do_order(){
    	$address_id = I('post.address_id');
    	if (!$address_id) {
    		$this->error('请选择一个收货地址！');
    	}
        $user = M('user')->where('id ='.$_SESSION['user_id'])->find();
        $address = M('user_address')->where('id ='.$address_id)->find();
    	$order_id = M('orders')->add(array(
    		'user_id' => $_SESSION['user_id'],
            'user_name' => $user['username'],
    		'address_id' => $address_id,
            'address_name' => $address['name'],
            'address_address' => $address['address'],
            'address_phone' => $address['phone'],
    		'create_time' => time(),
    		'status' => 0,
    	));
    	$product_id = I('post.product_id');
    	$num = I('post.num');
    	$count = count($product_id);
        $total = 0;
    	for ($i=0; $i < $count; $i++){
            $product = M('product')->where('id ='.$product_id[$i])->find();
            $total += intval($num[$i]) * floatval($product['price']);
    		M('orders_product')->add(array(
    			'order_id' => $order_id,
    			'product_id' => $product_id[$i],
                'product_title' => $product['title'],
                'product_price' => $product['price'],
                'product_cata_id' => $product['cata_id'],
    			'num' => $num[$i],
    		));
    	}
        M('orders')->where('id ='.$order_id)->save(array('total' => $total));
    	$this->success('提交订单成功',U('home/front/index'));
    }
    public function preOrder(){
    	$data = I('post.data');
    	if (count($data)==0) {
    	 	echo "请选择商品！";
    	}else{
    		$_SESSION['preorder'] = $data;
    		echo "ok";
    	}
    }
    public function addAddress(){
    	$name = I('post.name');
    	$address = I('post.address');
    	$phone = I('post.phone');
    	if (empty($name)) {
    		echo json_encode(array('code' => 'error','field' => 'name','msg' => '收件人不能为空'));
    		exit();
    	}
    	if (empty($address)) {
     		echo json_encode(array('code' => 'error','field' => 'address','msg' => '地址不能为空'));
     		exit();   		
    	}
    	$where = array(
    		'name' => $name,
    		'address' => $address,
    		'phone' => $phone,
    		'user_id' => $_SESSION['user_id']
    	);
    	M('user_address')->add($where);
    	echo json_encode(array('code'=>'ok'));
    }
    public function delete($id){
        M('user_address')->where('id ='.$id)->delete($id);
        $this->success('删除地址成功',U('home/shopping/order'));
    }
}